﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using AirTickets;

namespace AirTicketsWebservice
{
    /// <summary>
    /// Summary description for AirTickets
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class AirTickets : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string GetAvailableFlights(
            string departureCity,
            string arrivalCity,
            DateTime departureDate)
        {
            List<Vuelo> vuelos = Operaciones.BuscarVuelos(departureCity, arrivalCity, departureDate);

            StringBuilder sb = new StringBuilder("<?xml version=\"1.0\"?>\r\n<data>");
            sb.Append("<Vuelos>");
            foreach (Vuelo v in vuelos)
                sb.Append(v.GetXml());
            sb.Append("</Vuelos>");
            sb.Append("</data>");
            return sb.ToString();
        }

        [WebMethod]
        public string GetAvailableSeats(string flightCode)
        {
            List<Asiento> asientos = Operaciones.AsientosDisponibles(flightCode);
            StringBuilder sb = new StringBuilder("<?xml version=\"1.0\"?>\r\n<data>");
            sb.Append("<Asientos>");
            foreach (Asiento a in asientos)
                sb.Append(a.GetXml());
            sb.Append("</Asientos>");
            sb.Append("</data>");
            return sb.ToString();
        }

        [WebMethod]
        public string BookSeatInFligh(string flightCode, string seatCode)
        {
            StringBuilder sb = new StringBuilder("<?xml version=\"1.0\"?>\r\n<data>");
            try
            {
                Billete b = Operaciones.ObtenerBillete(flightCode, seatCode);
                sb.AppendFormat("<Billete>{0}</Billete>", b.Codigo);
            }
            catch (Exception ex)
            {
                sb.AppendFormat("<Resultado><Error>{0}</Error></Resultado>", ex.Message);
            }
            sb.Append("</data>");
            return sb.ToString();
        }

    }
}
